﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Threading.Tasks;

namespace FanaticRock.MediaSource.BilibiliAPI.Extensions
{
    public static class JsonExtension
    {
        public static string ToJson(this object obj)
        {
            return JsonSerializer.Serialize(obj);
        }

        public static string ToJsonWithIndent(this object obj)
        {
            return JsonSerializer.Serialize(obj, _writeIndentedOption.Value);
        }

        private static Lazy<JsonSerializerOptions> _writeIndentedOption = new Lazy<JsonSerializerOptions>(() =>
        {
            return new JsonSerializerOptions()
            {
                WriteIndented = true,
                Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, 
            };
        });
    }
}
